//
//  YCLStatusRetweetView.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/17.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLStatusRetweetView: UIView {
    
    //微博模型
    var status:YCLStatus? {
    
        didSet{
        
            let name = status?.user?.screen_name ?? "被转发微博的用户名称"
            let content = NSMutableAttributedString.init(string: "@\(name):")
            
//            let content = status?.text ?? "被转发微博内容为空"
            let text = status?.attrText ?? NSMutableAttributedString.init(string: "被转发微博内容为空")
            
//            let retweetText = "@\(name): \(content)"
            content.appendAttributedString(text)
            
            
            retweetLabel.attributedText = content
            
            retweetPictureView.pictureURLs = status?.storePictureURLs
            
            //若没有图片,重新更新约束
            retweetPictureView.snp_updateConstraints{ (make) in
               
                let margin = status?.storePictureURLs == nil ? 0 : YCLStatusCellMargin
                
                 make.top.equalTo(retweetLabel.snp_bottom).offset(margin)
            }
            
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置ui
        prepareUI()
        
    }
    
    func prepareUI() {
    
        addSubview(retweetButton)
        addSubview(retweetLabel)
        addSubview(retweetPictureView)
        
        //设置约束
        retweetButton.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        retweetLabel.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(self).offset(-YCLStatusCellMargin)
            make.top.leading.equalTo(self).offset(YCLStatusCellMargin)
        }
        
        retweetPictureView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(self).offset(YCLStatusCellMargin)
            make.top.equalTo(retweetLabel.snp_bottom).offset(YCLStatusCellMargin)
            
            //给cell固定一些高度
            make.size.equalTo(CGSizeZero)
        }
        
        self.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(retweetPictureView).offset(YCLStatusCellMargin)
        }
        
    }
    
    //懒加载控件
    
    //背景按钮
    private lazy var retweetButton: UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage.init(named: "retweet_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage.init(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
    }()
    
    //转发微博内容
    private lazy var retweetLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(YCLStatusContentFontSize)
        
        //设置换行
        label.numberOfLines = 0
        
        return label
    }()
    
    //转发的图片
    private lazy var retweetPictureView: YCLStatusPictureView = YCLStatusPictureView()
    

}
